#!/bin/bash

# 指定进程号
pid="6"

# 日志文件路径
log_file="top.log"

# 循环监控top命令输出并写入日志文件
while true; do
    # 获取当前时间
    current_time=$(date +"%Y-%m-%d %H:%M:%S")
    #top_output=$(top -b -n 1 -p "$pid")
    cpu_output=$(top -b -n 1 -p "$pid" | tail -n 1 | awk '{print $9}')
    me_output=$(top -b -n 1 -p "$pid" | tail -n 1 | awk '{print $10}')
    # 将top信息和时间写入日志文件
    echo "--------------------------------开始-----------------------------------" >> "$log_file"
    #echo "[$current_time] $top_output" >> "$log_file"
    echo "JAVA 进程 CPU: [$current_time] $cpu_output" >> "$log_file"
    echo "JAVA 进程 内存: [$current_time] $me_output" >> "$log_file"
    echo "--------------------------------结束-----------------------------------" >> "$log_file"
    #sleep 0.5
done